package Wlecome;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student {
    private String studentID;
    private String name;
    private List<Course> courses;
    private double gpa;

    public Student(String studentID, String name) {
        this.studentID = studentID;
        this.name = name;
        this.courses = new ArrayList<>();
    }

    public void addCourse(Course course) {
        courses.add(course);
    }

    public void calculateGPA() {
        double totalGradePoints = 0.0;
        double totalCredits = 0.0;
        for (Course course : courses) {
            totalGradePoints += course.getGradePoint() * course.getCredits();
            totalCredits += course.getCredits();
        }
        this.gpa = totalCredits > 0 ? totalGradePoints / totalCredits : 0.0;
    }

    public double getGPA() {
        calculateGPA();
        return gpa;
    }

    public String getID() {
        return studentID;
    }

    public String getName() {
        return name;
    }

    public List<Course> getCourses() {
        return courses;
    }
    public void printTranscript() {
        System.out.println("成绩单 - " + name + " (" + studentID + "):");
        System.out.println("课程ID\t课程名称\t学分\t分数\t等级");
        for (Course course : courses) {
            System.out.printf("%s\t\t%s\t\t%.1f\t%.1f\t%s\n",
                    course.getCourseID(), course.getName(), course.getCredits(),
                    course.getScore(), course.getLetterGrade());
        }
        System.out.printf("总 GPA: %.2f\n", getGPA());
    }

    public void exportTranscript(String filePath) {
        try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
            writer.println("成绩单 - " + name + " (" + studentID + "):");
            writer.println("课程ID\t课程名称\t学分\t分数\t等级");
            for (Course course : courses) {
                writer.printf("%s\t\t%s\t\t%.1f\t%.1f\t%s\n",
                        course.getCourseID(), course.getName(), course.getCredits(),
                        course.getScore(), course.getLetterGrade());
            }
            writer.printf("总 GPA: %.2f\n", getGPA());
            System.out.println("成绩单成功导出到 " + filePath);
        } catch (IOException e) {
            System.out.println("导出成绩单时出错: " + e.getMessage());
        }
    }
}

class Course {
    private String courseID;
    private String name;
    private double credits;
    private double score;
    private char letterGrade;

    public Course(String courseID, String name, double credits) {
        this.courseID = courseID;
        this.name = name;
        this.credits = credits;
    }
    public void setScore(double score) {
        this.score = score;
        this.letterGrade = calculateLetterGrade(score);
    }

    private char calculateLetterGrade(double score) {
        if (score >= 90) return 'A';
        else if (score >= 80) return 'B';
        else if (score >= 70) return 'C';
        else if (score >= 60) return 'D';
        else return 'F';
    }

    public double getGradePoint() {
        switch (letterGrade) {
            case 'A': return 4.0;
            case 'B': return 3.0;
            case 'C': return 2.0;
            case 'D': return 1.0;
            default: return 0.0;
        }
    }

    public String getCourseID() {
        return courseID;
    }

    public String getName() {
        return name;
    }

    public double getCredits() {
        return credits;
    }

    public double getScore() {
        return score;
    }

    public char getLetterGrade() {
        return letterGrade;
    }
}
public class GradeManagement {
    private List<Student> students = new ArrayList<>();

    public void addStudent(Student student) {
        students.add(student);
    }

    public void recordScore(String studentID, String courseID, double score) {
        for (Student student : students) {
            if (student.getID().equals(studentID)) {
                for (Course course : student.getCourses()) {
                    if (course.getCourseID().equals(courseID)) {
                        course.setScore(score);
                        student.calculateGPA();
                        System.out.println("成绩录入成功。");
                        return;
                    }
                }
            }
        }
        System.out.println("未找到该学生或课程。");
    }

    public void displayStudentGPA(String studentID) {
        Student student = getStudentByID(studentID);
        if (student == null) {
            System.out.println("未找到该学生。");
            return;
        }
        System.out.printf("学生 %s (%s) 的 GPA: %.2f\n", student.getName(), studentID, student.getGPA());
    }

    public void exportStudentTranscript(String studentID, String filePath) {
        Student student = getStudentByID(studentID);
        if (student != null) {
            student.exportTranscript(filePath);
        } else {
            System.out.println("未找到该学生。");
        }
    }

    private Student getStudentByID(String studentID) {
        for (Student student : students) {
            if (student.getID().equals(studentID)) {
                return student;
            }
        }
        return null;
    }
    public static void main(String[] args) {
        GradeManagement gm = new GradeManagement();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n--- 主菜单 ---");
            System.out.println("1. 添加学生");
            System.out.println("2. 录入学生成绩");
            System.out.println("3. 查看学生 GPA");
            System.out.println("4. 导出学生成绩单");
            System.out.println("5. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    System.out.print("请输入学生ID: ");
                    String studentID = scanner.nextLine();
                    System.out.print("请输入学生姓名: ");
                    String studentName = scanner.nextLine();
                    Student student = new Student(studentID, studentName);

                    System.out.print("请输入课程数量: ");
                    int courseCount = scanner.nextInt();
                    scanner.nextLine(); // consume newline

                    for (int i = 0; i < courseCount; i++) {
                        System.out.print("请输入课程ID: ");
                        String courseID = scanner.nextLine();
                        System.out.print("请输入课程名称: ");
                        String courseName = scanner.nextLine();
                        System.out.print("请输入课程学分: ");
                        double credits = scanner.nextDouble();
                        System.out.print("请输入课程成绩: ");
                        double score = scanner.nextDouble();
                        scanner.nextLine(); // consume newline

                        Course course = new Course(courseID, courseName, credits);
                        course.setScore(score);
                        student.addCourse(course);
                    }
                    gm.addStudent(student);
                    System.out.println("学生添加成功。");
                    break;

                case 2:
                    System.out.print("请输入学生ID: ");
                    String sid = scanner.nextLine();
                    System.out.print("请输入课程ID: ");
                    String cid = scanner.nextLine();
                    System.out.print("请输入成绩: ");
                    double score = scanner.nextDouble();
                    scanner.nextLine(); // consume newline
                    gm.recordScore(sid, cid, score);
                    break;

                case 3:
                    System.out.print("请输入学生ID: ");
                    String gpaStudentID = scanner.nextLine();
                    gm.displayStudentGPA(gpaStudentID);
                    break;

                case 4:
                    System.out.print("请输入学生ID: ");
                    String exportStudentID = scanner.nextLine();
                    System.out.print("请输入导出文件路径 (如: transcript.txt): ");
                    String filePath = scanner.nextLine();
                    gm.exportStudentTranscript(exportStudentID, filePath);
                    break;

                case 5:
                    System.out.println("程序已退出。");
                    scanner.close();
                    System.exit(0);

                default:
                    System.out.println("无效选择，请重新选择。");
            }
        }
    }
}